package com.imwenwen.controller;


import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imwenwen.pojo.*;
import com.imwenwen.service.*;
import com.imwenwen.util.JsoupUtil;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Imwenwen
 * @since 2020-12-19
 */
@Controller
@RequestMapping("/sys")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private LakercolumService lakercolumService;
    @Autowired
    private SportscolumService sportscolumService;
    @Autowired
    private BasketballcolumService basketballcolumService;
    @Autowired
    private FootballcolumService footballcolumService;
    @Autowired
    private FocuscolumService focuscolumService;
    @Autowired
    private FocusnewscolumService focusnewscolumService;
    @Autowired
    private FocusheadService focusheadService;


    @RequestMapping("/admin")
    public String admin() {
        return "admin/admin";
    }

    /**
     * 前往湖人栏目管理
     *
     * @return
     */
    @RequestMapping("/lakerColum")
    public String lakerColum() {
        return "admin/news/lakerColum";
    }

    /**
     * @return 获取湖人栏目Json数据格式
     */
    @RequestMapping("/lakerjson")
    @ResponseBody
    public Map<String, Object> lakerjson(HttpServletRequest request) {
        int PageNum = Integer.parseInt(request.getParameter("page"));
        int PageSize = Integer.parseInt(request.getParameter("limit"));
        Page<Lakercolum> page = new Page<>(PageNum, PageSize);
        IPage<Lakercolum> currentpage = lakercolumService.page(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", 100);
        map.put("data", currentpage.getRecords());
        return map;
    }

    /**
     * 获取湖人栏目今日新闻
     *
     * @return
     */
    @RequestMapping("/lakerobtiannowday")
    public String lakerobtiannowday(Model model) throws IOException {
        Elements ListDiv = JsoupUtil.jsoupbyclassname("http://sports.sina.com.cn/nba/", "news-list-a");
        int i = 0;
        for (Element div : ListDiv) {
            Elements links = div.getElementsByTag("li");
            if (i <= 15) {
                for (Element link : links) {
                    i++;
                    String linkHref = link.select("a").attr("href").trim();
                    String linkText = link.select("a").text().trim();
                    Lakercolum lakercolum = new Lakercolum();
                    lakercolum.setName(linkText);
                    lakercolum.setHref(linkHref);
                    lakercolumService.save(lakercolum);
                }
            } else {
                break;
            }
        }
        model.addAttribute("success", "更新成功");
        return "admin/news/lakerColum";
    }

    /**
     * 湖人栏目批量删除
     */
    @RequestMapping("/lakercolumbatchbyid")
    @ResponseBody
    public String lakercolumbatchbyid(String ids) {
        String msg = "";
        System.out.println(ids);
        String[] split = ids.split(",");
        for (String s : split) {
            QueryWrapper<Lakercolum> wrapper = new QueryWrapper<>();
            wrapper.eq("id", s);
            boolean flag = lakercolumService.remove(wrapper);
            if (flag) {
                msg = "OK";
            }
        }
        return msg;
    }

    /**
     * 前往体育栏目管理
     */
    @RequestMapping("/sportColum")
    public String sportColum() {
        return "admin/news/sportColum";
    }

    /**
     * @return 获取体育栏目Json数据格式
     */
    @RequestMapping("/sportjson")
    @ResponseBody
    public Map<String, Object> sportjson(HttpServletRequest request) {
        int PageNum = Integer.parseInt(request.getParameter("page"));
        int PageSize = Integer.parseInt(request.getParameter("limit"));
        Page<Sportscolum> page = new Page<>(PageNum, PageSize);
        IPage<Sportscolum> currentpage = sportscolumService.page(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", currentpage.getRecords());
        return map;
    }

    /**
     * 获取体育栏目今日新闻
     *
     * @return
     */
    @RequestMapping("/sportobtiannowday")
    public String sportobtiannowday(Model model) throws IOException {
        Elements ListDiv = JsoupUtil.jsoupbyid("https://news.sina.com.cn/", "blk_tyzl_01");
        for (Element div : ListDiv) {
            Elements links = div.getElementsByTag("li");
            for (Element link : links) {
                String linkHref = link.select("a").attr("href").trim();
                String linkText = link.select("a").attr("title").trim();
                Sportscolum sportscolum = new Sportscolum();
                sportscolum.setName(linkText);
                sportscolum.setHref(linkHref);
                sportscolumService.save(sportscolum);
            }
        }
        model.addAttribute("success", "更新成功");
        return "admin/news/sportColum";
    }

    /**
     * 体育栏目批量删除
     */
    @RequestMapping("/sportcolumbatchbyid")
    @ResponseBody
    public String sportcolumbatchbyid(String ids) {
        String msg = "";
        System.out.println(ids);
        String[] split = ids.split(",");
        for (String s : split) {
            QueryWrapper<Sportscolum> wrapper = new QueryWrapper<>();
            wrapper.eq("id", s);
            boolean flag = sportscolumService.remove(wrapper);
            if (flag) {
                msg = "OK";
            }
        }
        return msg;
    }


    /**
     * 前往NBA-篮球栏目管理
     *
     * @return
     */
    @RequestMapping("/bbColum")
    public String bbColum() {
        return "admin/news/basketballColum";
    }

    /**
     * @return 获取NBA-篮球栏目Json数据格式
     */
    @RequestMapping("/bbjson")
    @ResponseBody
    public Map<String, Object> bbjson(HttpServletRequest request) {
        int PageNum = Integer.parseInt(request.getParameter("page"));
        int PageSize = Integer.parseInt(request.getParameter("limit"));
        Page<Basketballcolum> page = new Page<>(PageNum, PageSize);
        IPage<Basketballcolum> currentpage = basketballcolumService.page(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", currentpage.getRecords());
        return map;
    }

    /**
     * 获取NBA-篮球栏目今日新闻
     *
     * @return
     */
    @RequestMapping("/bbobtiannowday")
    public String bbobtiannowday(Model model) throws IOException {

        Elements ListDiv = JsoupUtil.jsoupbyid("https://news.sina.com.cn/", "blk_tyxwlq_01");
        for (Element div : ListDiv) {
            Elements links = div.getElementsByTag("li");
            //System.out.println(links);
            for (Element link : links) {
                String linkHref = link.select("a").attr("href").trim();
                String linkText = link.select("a").text().trim();
                Basketballcolum basketballcolum = new Basketballcolum();
                basketballcolum.setName(linkText);
                basketballcolum.setHref(linkHref);
                basketballcolumService.save(basketballcolum);
            }
        }

        model.addAttribute("success", "更新成功");
        return "admin/news/basketballColum";
    }

    /**
     * 体育栏目批量删除
     */
    @RequestMapping("/bbcolumbatchbyid")
    @ResponseBody
    public String bbcolumbatchbyid(String ids) {
        String msg = "";
        System.out.println(ids);
        String[] split = ids.split(",");
        for (String s : split) {
            QueryWrapper<Basketballcolum> wrapper = new QueryWrapper<>();
            wrapper.eq("id", s);
            boolean flag = basketballcolumService.remove(wrapper);
            if (flag) {
                msg = "OK";
            }
        }
        return msg;
    }


    /**
     * 前往足球栏目管理
     *
     * @return
     */
    @RequestMapping("/fbColum")
    public String fbColum() {
        return "admin/news/footballColum";
    }

    /**
     * @return 获取足球栏目Json数据格式
     */
    @RequestMapping("/fbjson")
    @ResponseBody
    public Map<String, Object> fbjson(HttpServletRequest request) {
        int PageNum = Integer.parseInt(request.getParameter("page"));
        int PageSize = Integer.parseInt(request.getParameter("limit"));
        Page<Footballcolum> page = new Page<>(PageNum, PageSize);
        IPage<Footballcolum> currentpage = footballcolumService.page(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", currentpage.getRecords());
        return map;
    }

    /**
     * 获取足球栏目今日新闻
     *
     * @return
     */
    @RequestMapping("/fbobtiannowday")
    public String fbobtiannowday(Model model) throws IOException {

        Elements ListDiv = JsoupUtil.jsoupbyid("https://news.sina.com.cn/", "blk_tyxwgjzt_01");
        for (Element div : ListDiv) {
            Elements links = div.getElementsByTag("li");
            //System.out.println(links);
            for (Element link : links) {
                String linkHref = link.select("a").attr("href").trim();
                String linkText = link.select("a").text().trim();
                Footballcolum footballcolum = new Footballcolum();
                footballcolum.setName(linkText);
                footballcolum.setHref(linkHref);
                footballcolumService.save(footballcolum);
            }
        }

        model.addAttribute("success", "更新成功");
        return "admin/news/footballColum";
    }

    /**
     * 足球栏目批量删除
     */
    @RequestMapping("/fbcolumbatchbyid")
    @ResponseBody
    public String fbcolumbatchbyid(String ids) {
        String msg = "";
        System.out.println(ids);
        String[] split = ids.split(",");
        for (String s : split) {
            QueryWrapper<Footballcolum> wrapper = new QueryWrapper<>();
            wrapper.eq("id", s);
            boolean flag = footballcolumService.remove(wrapper);
            if (flag) {
                msg = "OK";
            }
        }
        return msg;
    }

    /**
     * 前往要闻栏目管理
     *
     * @return
     */
    @RequestMapping("/focusnewsColum")
    public String focusnewsColum() {
        return "admin/news/focusnewsColum";
    }

    /**
     * @return 获取要闻栏目Json数据格式
     */
    @RequestMapping("/focusnewsjson")
    @ResponseBody
    public Map<String, Object> focusnewsjson(HttpServletRequest request) {
        int PageNum = Integer.parseInt(request.getParameter("page"));
        int PageSize = Integer.parseInt(request.getParameter("limit"));
        Page<Focusnewscolum> page = new Page<>(PageNum, PageSize);
        IPage<Focusnewscolum> currentpage = focusnewscolumService.page(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", currentpage.getRecords());
        return map;
    }

    /**
     * 获取要闻栏目今日新闻
     *
     * @return
     */
    @RequestMapping("/fnobtiannowday")
    public String fnobtiannowday(Model model) throws IOException {

        Elements ListDiv = JsoupUtil.jsoupbyclassname("https://news.sina.com.cn/", "ct_t_01");
        for (Element div : ListDiv) {
            Elements links = div.getElementsByTag("a");

            for (Element link : links) {
                String linkHref = link.attr("href").trim();
                String linkText = link.text().trim();
                Focusnewscolum focusnewscolum = new Focusnewscolum();
                focusnewscolum.setTitle(linkText);
                focusnewscolum.setTitleUrl(linkHref);
                focusnewscolumService.save(focusnewscolum);
            }
        }

        model.addAttribute("success", "更新成功");
        return "admin/news/focusnewsColum";
    }

    /**
     * 要闻栏目批量删除
     */
    @RequestMapping("/fncolumbatchbyid")
    @ResponseBody
    public String fncolumbatchbyid(String ids) {
        String msg = "";
        System.out.println(ids);
        String[] split = ids.split(",");
        for (String s : split) {
            QueryWrapper<Focusnewscolum> wrapper = new QueryWrapper<>();
            wrapper.eq("id", s);
            boolean flag = focusnewscolumService.remove(wrapper);
            if (flag) {
                msg = "OK";
            }
        }
        return msg;
    }

    /**
     * 前往焦点栏目管理
     */
    @RequestMapping("/focusColum")
    public String focusColum() {
        return "admin/news/focusColum";
    }

    /**
     * @return 获取焦点栏目Json数据格式
     */
    @RequestMapping("/fscusnewsjson")
    @ResponseBody
    public Map<String, Object> fscusnewsjson(HttpServletRequest request) {
        int PageNum = Integer.parseInt(request.getParameter("page"));
        int PageSize = Integer.parseInt(request.getParameter("limit"));
        Page<Focuscolum> page = new Page<>(PageNum, PageSize);
        IPage<Focuscolum> currentpage = focuscolumService.page(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", currentpage.getRecords());
        return map;
    }

    /**
     * 获取焦点栏目今日新闻
     * 焦点新闻导航栏的标题
     *
     * @return
     */
    @RequestMapping("/fsobtiannowday")
    public String fsobtiannowday(Model model) throws IOException {

        Elements ListDiv = JsoupUtil.jsoupbyid("https://news.sina.com.cn/", "Blk01_Focus_Cont");
        //System.out.println(ListDiv);
        for (Element div : ListDiv) {
            Elements links = div.getElementsByTag("li");
            System.out.println(links);
            for (Element link : links) {
                String Img = link.select("img").attr("src").trim();
                String name = link.select("div").select("a").text().trim();
                String Href = link.select("div").select("a").attr("href").trim();
                Focuscolum focuscolum = new Focuscolum();
                focuscolum.setTitle(name);
                focuscolum.setTitleHref(Href);
                focuscolum.setTitleImg(Img);
                focuscolumService.save(focuscolum);
            }
        }
        Elements ListDiv2 = JsoupUtil.jsoupbyid("https://news.sina.com.cn/", "Blk01_Focus_Nav");
        for (Element div : ListDiv2) {
            Elements links = div.getElementsByTag("li");
            for (Element link : links) {
                String linkText = link.text().trim();
                Focushead focushead = new Focushead();
                focushead.setFocusHead(linkText);
                focusheadService.save(focushead);
            }
        }
        model.addAttribute("success", "更新成功");
        return "admin/news/focusColum";
    }

    /**
     * 焦点栏目批量删除
     */
    @RequestMapping("/fscolumbatchbyid")
    @ResponseBody
    public String fscolumbatchbyid(String ids) {
        String msg = "";
        System.out.println(ids);
        String[] split = ids.split(",");
        for (String s : split) {
            QueryWrapper<Focuscolum> wrapper = new QueryWrapper<>();
            wrapper.eq("id", s);
            boolean flag = focuscolumService.remove(wrapper);
            if (flag) {
                msg = "OK";
            }
        }
        return msg;
    }

    /**
     * @return 返回管理员个人资料
     */
    @RequestMapping("/admininfo")
    private String admininfo(Model model,HttpSession session) {
        model.addAttribute("admin",session.getAttribute("admin") );
        return "admin/setup/admin_info";
    }

    /**
     * uploadPictures:上传图片返回JSON数据格式
     */
    @RequestMapping("/uploadPictures")
    @ResponseBody
    public Map<String, Object> uploadPictures(MultipartFile file, HttpServletRequest request)
            throws IOException {
//        String root = request.getRealPath("/");
        String root=System.getProperty("user.dir");
        String path = "\\img\\";
//        String paths = root + path;
        String paths=root+ "/target/classes/static/"+path;
        String fileName = file.getOriginalFilename();
        File filePath = new File(paths, fileName);
        System.out.println("文件路径" + filePath);
        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
        }
        file.transferTo(filePath);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        Map<String, Object> data = new HashMap<>();
        data.put("src", path + fileName);
        map.put("data", data);
        return map;

    }

    /**
     * info：修改管理员基本资料控制器
     * package org.apache.commons.beanutils;（常用）
     * BeanUtils.copyProperties(A,B);
     * 是B中的值付给A
     * <p>
     * saveOrUpdate
     * // TableId 注解存在更新记录，否插入一条记录
     * boolean saveOrUpdate(T entity);
     * // 根据updateWrapper尝试更新，否继续执行saveOrUpdate(T)方法
     * boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
     * // 批量修改插入
     * boolean saveOrUpdateBatch(Collection<T> entityList);
     * // 批量修改插入
     * boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
     */
    @RequestMapping("/info")
    public String info(Admin admin,HttpSession session) {
        Admin admins = (Admin) session.getAttribute("admin");
        admins.setEmail(admin.getEmail());
        admins.setMobile(admin.getMobile());
        admins.setNickname(admin.getNickname());
        if (!Objects.equals(admin.getHead(), "")) {
            admins.setHead(admin.getHead());
        }
        adminService.saveOrUpdate(admins);
        session.setAttribute("admin",admins);
        return "redirect:/sys/admininfo";
    }

    /**
     * 管理员重置密码
     */
    @RequestMapping("/adminpassword")
    private String adminpassword(Model model,HttpSession session) {
        model.addAttribute("admin", session.getAttribute("admin"));
        return "admin/setup/admin_password";
    }

    /**
     * 参数 更改的用户id、旧密码、新密码、确认密码
     * 管理员重置密码
     * 表单提交控制器
     */
    @RequestMapping("/password")
    private String password(Model model, HttpServletRequest request) {
        String oldPassword = request.getParameter("oldPassword");
        String newPassword = request.getParameter("newPassword");
        String confirmPassword = request.getParameter("confirmPassword");
        String id = request.getParameter("id");
        oldPassword = DigestUtil.md5Hex(oldPassword);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("password", oldPassword);
        wrapper.allEq(map);
        List<Admin> admin = adminService.list(wrapper);
        if (admin.size() == 0) {
            model.addAttribute("errorold", "原密码输入错误");
        }
        if (!Objects.equals(newPassword, confirmPassword)) {
            model.addAttribute("errornewpwd", "两次密码不一致");
        }
        if (Objects.equals(newPassword, confirmPassword) && admin.size() > 0) {
            admin.get(0).setPassword(DigestUtil.md5Hex(newPassword));
            adminService.saveOrUpdate(admin.get(0));
            model.addAttribute("success", "修改密码成功");
        }
        model.addAttribute("admin", adminService.getById(1L));
        return "admin/setup/admin_password";
    }

    /**
     * 查看个人资料
     */
    @RequestMapping("/adminLookInfo")
    public String adminLookInfo(Model model,HttpSession session) {
        model.addAttribute("admin", session.getAttribute("admin"));
        return "admin/setup/admin_LookInfo";
    }

    /**
     * 注销
     *
     * @param session
     */
    @RequestMapping("/adminLogout")
    public String adminlogout(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }

    /**
     * 用户管理界面
     * 用户管理界面-网站用户管理
     */
    @RequestMapping("/userList")
    public String userList() {
        return "admin/user/userList";
    }

    /**
     * 用户管理界面
     * 用户管理界面-返回用户JSON数据
     */
    @RequestMapping("/userListJson")
    @ResponseBody
    public Map<String, Object> userListJson(HttpServletRequest request) {
        int PageNum = Integer.parseInt(request.getParameter("page"));
        int PageSize = Integer.parseInt(request.getParameter("limit"));
        Page<User> page = new Page<>(PageNum, PageSize);
        IPage<User> currentpage = userService.page(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", currentpage.getRecords());
        return map;

    }

    /**
     * 用户管理界面
     * 用户管理界面- 用户根据id查询
     */
    @RequestMapping("/userListSearch")
    @ResponseBody
    public Map<String, Object> userListSearch(HttpServletRequest request) {
        int PageNum = Integer.parseInt(request.getParameter("page"));
        int PageSize = Integer.parseInt(request.getParameter("limit"));
        int id = Integer.parseInt(request.getParameter("orderId"));
        Page<User> page = new Page<>(PageNum, PageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        IPage<User> currentpage = userService.page(page, wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", 100);
        map.put("data", currentpage.getRecords());
        return map;

    }

    /**
     * 用户管理界面
     * 用户管理界面- 用户根据id删除用户
     * 返回是否删除成功
     */
    @RequestMapping("/userDeleted")
    @ResponseBody
    public String userDeleted(Integer id) {
        userService.removeById(id);
        String msg;
        if (userService.removeById(id)) {
            msg = "删除成功";
        } else {
            msg = "删除失败";
        }
        return msg;

    }

    /**
     * 用户管理界面
     * 用户管理界面- 用户根据id重置用户密码123456
     */
    @RequestMapping("/userPassword")
    @ResponseBody
    public String respsdAdmin(String id) {
        String msg;
        User user = userService.getById(id);
        String password = DigestUtil.md5Hex("123456");
        user.setPassword(password);
        userService.saveOrUpdate(user);
        if (userService.saveOrUpdate(user)) {
            msg = "重置密码成功";
        } else {
            msg = "重置密码失败";
        }
        return msg;
    }

    /**
     * 用户管理界面
     * 用户管理界面- 用户根据id编辑用户信息
     */
    @RequestMapping("/userEdits")
    public String userEdits(Integer id, Model model) {
        model.addAttribute("user", userService.getById(id));
        return "admin/user/userforms/userEdits";
    }

    /**
     * 用户管理界面
     * 用户管理界面- 用户编辑表单提交控制器
     */
    @RequestMapping("/userEditsPost")
    @ResponseBody
    public String userEditsPost(User user) {
        User user1 = userService.getById(user.getId());
        user1.setUsername(user.getUsername());
        user1.setNickname(user.getNickname());
        user1.setTelephone(user.getTelephone());
        user1.setAge(user.getAge());
        user1.setHead(user.getHead());
        user1.setLocked(user.getLocked());
        userService.saveOrUpdate(user1);
        String msg;
        if (userService.saveOrUpdate(user1)) {
            msg = "编辑成功";
        } else {
            msg = "编辑失败";
        }
        return msg;
    }

    /**
     * 用户管理界面
     * 用户管理界面-导航栏添加功能界面
     */
    @RequestMapping("/userAdd")
    public String userAdd() {
        return "admin/user/userforms/userAdd";
    }

    /**
     * 用户管理界面
     * 用户管理界面-导航栏添加表单
     * 返回添加用户的JSON数据
     * msg
     */
    @RequestMapping("/userListAdd")
    @ResponseBody
    public String userListAdd(User user) {
        user.setPassword(DigestUtil.md5Hex(user.getPassword()));
         userService.save(user);
        return "添加成功";
    }
    /**
     * 用户管理界面
     * 用户管理界面-导航栏批量删除功能
     */
    @RequestMapping("/userBatchDeleted")
    @ResponseBody
    public String userBatchDeleted(String ids) {
        String msg = "";
        System.out.println(ids);
        String[] split = ids.split(",");
        for (String s : split) {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("id", s);
            boolean flag = userService.remove(wrapper);
            if (flag) {
                msg = "已删除";
            } else {
                msg = "删除失败";
            }
        }
        return msg;
    }

    /**
     * 用户管理界面
     * 用户管理界面-网站用户管理
     */
    @RequestMapping("/adminList")
    public String adminList() {
        return "admin/user/adminList";
    }

    /**
     * 用户管理界面
     * 用户管理界面-返回用户JSON数据
     */
    @RequestMapping("/adminListJson")
    @ResponseBody
    public Map<String, Object> adminListJson(HttpServletRequest request) {
        int PageNum = Integer.parseInt(request.getParameter("page"));
        int PageSize = Integer.parseInt(request.getParameter("limit"));
        Page<Admin> page = new Page<>(PageNum, PageSize);
        IPage<Admin> currentpage = adminService.page(page, null);
        System.out.println("---------->>>"+currentpage.getRecords());
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", currentpage.getRecords());
        return map;

    }

    /**
     * 用户管理界面
     * 用户管理界面- 用户根据id查询
     */
    @RequestMapping("/adminListSearch")
    @ResponseBody
    public Map<String, Object> adminListSearch(HttpServletRequest request) {
        int PageNum = Integer.parseInt(request.getParameter("page"));
        int PageSize = Integer.parseInt(request.getParameter("limit"));
        int id = Integer.parseInt(request.getParameter("orderId"));
        Page<Admin> page = new Page<>(PageNum, PageSize);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        IPage<Admin> currentpage = adminService.page(page, wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", 100);
        map.put("data", currentpage.getRecords());
        return map;

    }

    /**
     * 用户管理界面
     * 用户管理界面- 用户根据id删除用户
     * 返回是否删除成功
     */
    @RequestMapping("/adminDeleted")
    @ResponseBody
    public String adminDeleted(Integer id) {
        adminService.removeById(id);
        String msg;
        if (adminService.removeById(id)) {
            msg = "删除成功";
        } else {
            msg = "删除失败";
        }
        return msg;

    }

    /**
     * 用户管理界面
     * 用户管理界面- 用户根据id重置用户密码123456
     */
    @RequestMapping("/adminPassword")
    @ResponseBody
    public String adminPassword(String id) {
        String msg;
       Admin admin = adminService.getById(id);
        String password = DigestUtil.md5Hex("123456");
        admin.setPassword(password);
        adminService.saveOrUpdate(admin);
        if (adminService.saveOrUpdate(admin)) {
            msg = "重置密码成功";
        } else {
            msg = "重置密码失败";
        }
        return msg;
    }

    /**
     * 管理员管理界面
     * 管理员管理界面- 管理员根据id编辑用户信息
     */
    @RequestMapping("/adminEdits")
    public String adminEdits(Integer id, Model model) {
        model.addAttribute("admin", adminService.getById(id));
        return "admin/user/adminforms/adminEdits";
    }

    /**
     * 管理员管理界面
     * 管理员管理界面 管理员编辑表单提交控制器
     */
    @RequestMapping("/adminEditsPost")
    @ResponseBody
    public String adminEditsPost(Admin admin) {
        Admin admin1 = adminService.getById(admin.getId());
        admin1.setUsername(admin.getUsername());
        admin1.setNickname(admin.getNickname());
        admin1.setEmail(admin.getEmail());
        admin1.setMobile(admin.getMobile());
        admin1.setHead(admin.getHead());
        admin1.setEnabled(admin.getEnabled());
        adminService.saveOrUpdate(admin1);
        String msg;
        if (adminService.saveOrUpdate(admin1)) {
            msg = "编辑成功";
        } else {
            msg = "编辑失败";
        }
        return msg;
    }

    /**
     * 管理员管理界面
     * 管理员管理界面-导航栏添加功能界面
     */
    @RequestMapping("/adminAdd")
    public String adminAdd() {
        return "admin/user/adminforms/adminAdd";
    }

    /**
     * 管理员管理界面
     * 管理员管理界面-导航栏添加表单
     * 返回添加用户的JSON数据
     * msg
     */
    @RequestMapping("/adminListAdd")
    @ResponseBody
    public String adminListAdd(Admin admin)  {
        admin.setPassword(DigestUtil.md5Hex(admin.getPassword()));
//        String realPath = request.getServletContext().getRealPath("/img");
//        File files = new File(realPath + "/" + file.getOriginalFilename());
//        if (!files.getParentFile().exists()) {
//            files.getParentFile().mkdirs();
//        }
//        file.transferTo(files);
        adminService.save(admin);
        return "添加成功";
    }
    /**
     * 管理员管理界面
     * 管理员管理界面-导航栏批量删除功能
     */
    @RequestMapping("/adminBatchDeleted")
    @ResponseBody
    public String adminBatchDeleted(String ids) {
        String msg = "";
        System.out.println(ids);
        String[] split = ids.split(",");
        for (String s : split) {
            QueryWrapper<Admin> wrapper = new QueryWrapper<>();
            wrapper.eq("id", s);
            boolean flag = adminService.remove(wrapper);
            if (flag) {
                msg = "已删除";
            } else {
                msg = "删除失败";
            }
        }

        return msg;
    }


    /**
     * 跳转登录页面
     *
     */

    @RequestMapping("/login")
    private String login(){
        return "admin/login";
    }

    /**
     * 用户登录
     *userlogin
     */
    @RequestMapping("/userlogin")
    public String userlogin(HttpServletRequest request, Model model, HttpSession session) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        password = DigestUtil.md5Hex(password);
        QueryWrapper<User> userwrapper=new QueryWrapper<>();
        QueryWrapper<Admin> adminwrapper=new QueryWrapper<>();
        Map<String, Object> usermap = new HashMap<>();
        Map<String, Object> adminmap = new HashMap<>();
        usermap.put("username", username);
        usermap.put("password", password);
        userwrapper.allEq(usermap);
        adminmap.put("username", username);
        adminmap.put("password", password);
        adminwrapper.allEq(adminmap);
        List<User> user=userService.list(userwrapper);
        List<Admin> admin=adminService.list(adminwrapper);
        if (admin.size()==0&&user.size()==0) {
            model.addAttribute("error", "账号密码错误");
            return "admin/login";
        } else {
            if (admin.size()>0) {
                if (admin.get(0).getEnabled().endsWith("N")) {
                    session.setAttribute("admin", admin.get(0));
                    return "redirect:/index";
                } else {
                    model.addAttribute("error", "该账号已被锁定请联系管理员解控");
                    return "admin/login";
                }
            } else {
                if (user.get(0).getLocked().endsWith("N")) {
                    session.setAttribute("user", user.get(0));
                    return "redirect:/index";
                }else {
                    model.addAttribute("error", "该账号已被锁定请联系管理员解控");
                    return "admin/login";
                }
            }
        }
    }

    /**
     * 跳转注册界面
     *
     */
    @RequestMapping("/reg")
    private String  reg(){
        return "admin/reg";
    }
    /**
     * 注册界面表单
     *
     */
    @RequestMapping("/Register")
    private String Register(User user,Model model){
      boolean flag= userService.saveOrUpdate(user);
      if(flag) {
          model.addAttribute("success", "注册成功");
      }
      else {
          model.addAttribute("regerror","注册失败请重新尝试");
          return "redirect:/sys/reg";
      }
        return "admin/login";
    }

/**
 * 404页面
 *
 */
@RequestMapping("/404")
private String error(){
    return "error/404";
    }

    /**
     * error页面
     *
     */
    @RequestMapping("/errors")
    private String errors(){
        return "error/error";
    }
    /**
     * 关于我
     *
     */
    @RequestMapping("/about")
    private String about(){
        return "admin/about";
    }
}

